package com.yunchao.feature.lock.domain.model

import com.yunchao.library.lock.LockEPCInfo
import com.yunchao.library.lock.getOperatorTime
import com.yunchao.library.lock.getVoucherTypeName
import com.yunchao.library.lock.hexToBinary

internal data class LockLogModel(
    val lockLogTitle: String,
    val lockOperator: String,
    val lockAuth: String,
    val operatorTime: String,
    val pageNumber: Int? = null,
    val voucherTypeName: String? = null,
    val deviceId: String? = null,
    val operatorResult: String? = null,
    val isLogistics: Boolean = false
) {
    companion object {
        fun getLogisticsItems(items: List<String>): List<LockLogModel> =
            items.mapNotNull { getLockLogisticsItem(it) }

        fun getLogItems(items: List<String>, lockEPCInfo: LockEPCInfo): List<LockLogModel> =
            items.mapNotNull { getLockLogItem(it, lockEPCInfo) }
    }
}

internal fun getLockLogisticsItem(value: String): LockLogModel? {
    val lockLogTitle = getLockCmdType(value.substring(0, 2))
    return if (lockLogTitle.isNotBlank() && value.length >= 26) {
        val lockOperator = value.substring(2, 10).toInt(16).toString()
        val lockAuth = value.substring(10, 18).toInt(16).toString()
        val operatorTime = getOperatorTime(hexToBinary(value.substring(18, 26)))
        LockLogModel(
            lockLogTitle = lockLogTitle,
            lockOperator = lockOperator,
            lockAuth = lockAuth,
            operatorTime = operatorTime,
            isLogistics = true
        )
    } else {
        null
    }
}

internal fun getLockLogItem(value: String, lockEPCInfo: LockEPCInfo): LockLogModel? {
    val lockLogTitle = getLockCmdType(value.substring(0, 2))
    return if (lockLogTitle.isNotBlank() && value.length >= 48) {
        val pageNumber = value.substring(2, 6).toInt(16)
        val lockOperator = value.substring(6, 14).toInt(16).toString()
        val lockAuth = value.substring(14, 22).toInt(16).toString()
        val operatorTime = getOperatorTime(hexToBinary(value.substring(22, 30)))

        val bits = hexToBinary(value.substring(30, 36))
        val deviceId = bits.substring(0, 20).toInt(2).toString()
        val voucherTypeName = lockEPCInfo.getVoucherTypeName()
        val operatorResult = getOperatorResult(value.substring(46, 48))

        LockLogModel(
            lockLogTitle = lockLogTitle,
            lockOperator = lockOperator,
            lockAuth = lockAuth,
            operatorTime = operatorTime,
            pageNumber = pageNumber,
            voucherTypeName = voucherTypeName,
            deviceId = deviceId,
            operatorResult = operatorResult,
        )
    } else {
        null
    }

}


internal fun getOperatorResult(operatorCode: String) = when (operatorCode) {
    "80" -> "开始操作"
    "40" -> "操作成功"
    "20" -> "操作失败"
    "10" -> "操作异常"
    else -> "未知"
}

internal fun getLockCmdType(cmdType: String) = when (cmdType) {
    "F1" -> "签封激活指令"
    "B1" -> "关锁指令"
    "B2" -> "开锁指令"
    "B3" -> "关锁写物流指令"
    "B4" -> "开锁写物流指令"
    "C1" -> "追溯指令"
    "C9" -> "追溯响应指令"
    "D1" -> "恢复指令"
    "E1" -> "日志清除指令"
    else -> ""
}
